<?php
namespace lightblog\home\model;

use lightblog\engine\activeRecord;

class commentModel extends activeRecord {
    public function newComment() {
        $post = \lightblog::$app->request->parameter->post;

        $article = $post['article'];
        $content = $post['content'];
        $author = $post['cuser'];
        $email = '1005411480a@gmail.com';
        $time = date('Y-m-d H:i:s');
        $pcomment = 0;

        if ($this->validate($post, 'new_comment')) {
            $sql_fields = "`article`, `author`, `email`, `content`, `time`, `pcomment`";
            $sql_values = "$article, '$author', '$email', '$content', '$time', $pcomment";
            $sql = "INSERT INTO `comments` ($sql_fields) VALUES ($sql_values)";
            $res = $this->insert($sql);
            return $res;
        }
        return false;
    }

    /**
     * 得到某片文章的评论
     *
     * @param integer $aid 文章编号
     */
    public function getArticleComment($aid) {
        $sql = "SELECT * FROM `comments` WHERE article = $aid";

        $comments = $this->select($sql);
        foreach ($comments as $k => $v) {
            $comments[$k]['time'] = date('M d, Y H:i:s', strtotime($v['time']));
        }

        return $comments;
    }

    /* 通用场景验证函数
     */
    public function validate_all($data) {
    }

    /* 场景的验证函数
     * TODO: 添加验证规则
     */
    public function validate_new_comment($data) {
        return true;
    }
}
